<?php
	function getHeaderCart($ajax = false)
	{
		$productCount = 0;
		$totalPrice = 0;
		$transportPrice = 0;
		
		$products = getCartProducts();
		if(isset($products[0]))
		{
			$totalPrice = $products[0]["grand_total"];
			$transportPrice = $products[0]["transport_price"];
			$productCount = $products[0]["total_products"];
		}
		
		$file = "global-includes/cart.php";
		if($ajax == true)
		{
			$file = "../../".$file;
		}
		require_once($file);
	}
	
	function getCartProducts()
	{
		$products = array(); 
		if(isset($_SESSION["cart"]) && count($_SESSION["cart"]) > 0)
		{
			$totalPrice = 0;
			$productCount = 0;
			$discountValue = 0;
			$discounts = array();
			
			if(isset($_SESSION["coupons"]) && count($_SESSION["coupons"]) > 0)
			{
				foreach($_SESSION["coupons"] as $key => $coupon) 
				{
					$voucher = getCouponDiscount($coupon);
					if(!isset($voucher["id"]))
					{
						unset($_SESSION["coupons"][$key]);
					}
					else
					{
						$discounts[$coupon] = $voucher;
					}
				}
			}
			
			foreach($_SESSION["cart"] as $productID => $stocks)
			{
				foreach($stocks as $stockID => $quantity)
				{
					$product = getProductByField($stockID, "stock_id");
					$product["max_quantity"] = $product["quantity"];
					$product["quantity"] = $quantity;
					
					$product["total_price"] = $product["min_price"] * $quantity;
					
					$products[] = $product;
					$totalPrice += $product["total_price"];
					$productCount+= $quantity;
					
					
					if(count($discounts) > 0)
					{
						foreach ($discounts as $key => $coupon) 
						{
							if($coupon["product_id"] == $product["id"] && !isset($coupon["already_in_use"]))
							{
								if($coupon["start"] <= time() && time() <= $coupon["end"])
								{
									if($coupon["type"] == 0)
									{
										$productDiscount = ($product["min_price"]*$coupon["discount"])/100;
									}
									else 
									{
										$productDiscount = $coupon["discount"];
									}
									$discountValue += $productDiscount;
									
									$discounts[$key]["value"] = $productDiscount;
									$discounts[$key]["already_in_use"] = 1;
								}
								else
								{
									unset($_SESSION["coupons"][$key]);
								}
							}
						}
					}
				}
			}
			if(isset($products[0]["id"]))
			{
				
				$transportPrice = get_setting("transport_price");
				if($totalPrice >= $transportPrice)
				{
					$transportPrice = 0;
				}
				
				$products[0]["products_price"] = $totalPrice - $discountValue;
				$products[0]["discounts"] = $discounts;
				$totalPrice += $transportPrice;
				
				$products[0]["transport_price"] = $transportPrice;
				$products[0]["grand_total"] = $totalPrice - $discountValue;
				$products[0]["total_products"] = $productCount;
				
				
				$rewardValue = get_setting("thankyou_value");
				
				if($rewardValue > 0)
				{
					$client = getLoginData();
					if($client["reward_points"] > 0)
					{
						$points = $client["reward_points"];
						
						$rewardDiscount = $points * $rewardValue;
						
						if($rewardDiscount > $products[0]["grand_total"])
						{
							$points = $products[0]["grand_total"] / $rewardValue;
							$rewardDiscount = $products[0]["grand_total"];
						}
						
						$products[0]["reward_discount"] = array("points" => $points, "value" => $rewardDiscount);
						
						$products[0]["grand_total"] = $products[0]["grand_total"] - $rewardDiscount;
						
					}
				}	
				
			}
		}
		
		return $products;
	}

	function getCouponDiscount($coupon)
	{
		global $connection;
		$params[] = $coupon;
		$query = "";
		
		$clientID = 0;
		if(isset($_SESSION["client"]["id"]))
		{
			$clientID = $_SESSION["client"]["id"];
		}
		
		$params[] = $clientID;
		
		$coupon = $connection->fetch_one("SELECT * FROM discounts WHERE coupon_code = ? AND client_id = ?", $params);
		
		return $coupon;
	}
?>